// DlgFormat.cpp : file di implementazione
//

#include "stdafx.h"
#include "Univue.h"
#include "DlgFormat.h"
#include "afxdialogex.h"
#include "drivefunc.h"
#include "func.h"


// finestra di dialogo CDlgFormat

IMPLEMENT_DYNAMIC(CDlgFormat, CDialog)

CDlgFormat::CDlgFormat(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgFormat::IDD, pParent)
{

}

CDlgFormat::~CDlgFormat()
{
}

void CDlgFormat::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_FORMAT, m_ctrlFormatDrive);
}

BEGIN_MESSAGE_MAP(CDlgFormat, CDialog)
	ON_BN_CLICKED(IDOK, &CDlgFormat::OnBnClickedOk)
	ON_BN_CLICKED(IDCANCEL, &CDlgFormat::OnBnClickedCancel)
	ON_BN_CLICKED(IDC_BUTTON_FORMAT, &CDlgFormat::OnBnClickedButtonFormat)
	ON_WM_SHOWWINDOW()
	ON_WM_TIMER()
END_MESSAGE_MAP()

// gestori di messaggi CDlgFormat

void CDlgFormat::OnBnClickedOk()
{
	// TODO: aggiungere qui il codice per la gestione della notifica del controllo.
	CDialog::OnOK();
}


void CDlgFormat::OnBnClickedCancel()
{
	// TODO: aggiungere qui il codice per la gestione della notifica del controllo.
	CDialog::OnCancel();
}


void CDlgFormat::OnBnClickedButtonFormat()
{
	CString szText, szDrv = _T("D:");
	int nIndex = m_ctrlFormatDrive.GetCurSel();
	m_ctrlFormatDrive.GetWindowText( szText );
	szDrv.SetAt(0, szText.GetAt(0));
	szDrv.SetAt(1, szText.GetAt(1));

	formatDrive( m_hWnd, szDrv.GetBuffer(0) );
	PostMessage( WM_CLOSE, 0, 0 );
	//_tprintf(_T("\n--->>> [%s]"), szDrv.GetBuffer(0) );
	// TODO: aggiungere qui il codice per la gestione della notifica del controllo.
}

BOOL CDlgFormat::OnInitDialog()
{
	CDialog::OnInitDialog();
	SetTimer( 1, 500, 0 );
	m_ctrlFormatDrive.SendMessage(EM_SETREADONLY ,1 ,0);
	return TRUE;
}

void CDlgFormat::OnShowWindow(BOOL bShow, UINT nStatus)
{
	CDialog::OnShowWindow(bShow, nStatus);
	if( bShow )
	{
		SetTimer( 1, 500, 0 );
	}
	else
	{

	}
}

CString CDlgFormat::GetVolumeName(CString szDrv )
{
	TCHAR vol[40] = {0,}, drive[100] = {0,};        
	DWORD mf;            
	DWORD sf,sno;    
	GetVolumeInformation(szDrv.GetBuffer(0),
							vol,
							sizeof(vol),
							&sno,
							&mf,
							&sf,
							NULL,NULL);
	return vol;
}


void CDlgFormat::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: aggiungere qui il codice per la gestione dei messaggi e/o chiamare il codice predefinito.

	switch( nIDEvent )
	{
	case 1:
		{
			KillTimer(1);
			int nDrvIndex =0;
			CDriveFunc::ReInitUSB_Disk_Letter();
			CString szDrive, szDriveTemp, szVol;
			szDrive.Format(L"%s", CDriveFunc::szMoveDiskName );
			szDriveTemp.Format(L"%s", CDriveFunc::szDrvName );

			m_ctrlFormatDrive.ResetContent();

			for( int nDrvIndex=0; nDrvIndex< szDrive.GetLength(); nDrvIndex++)
			{
				if( nDrvIndex == 0 )	continue;
				if( szDrive.Mid(nDrvIndex, 1) ==_T('A') || szDrive.Mid(nDrvIndex, 1) ==_T('B') )	continue;
				szVol = GetVolumeName( szDrive.Mid(nDrvIndex, 1) + _T(":\\") );
				int nRet = m_ctrlFormatDrive.AddString(szDrive.Mid(nDrvIndex, 1) + _T(": ") + szVol );
			}
			m_ctrlFormatDrive.SetCurSel(0);
			m_ctrlFormatDrive.Invalidate();
		}
		break;

	}
	CDialog::OnTimer(nIDEvent);
}
